<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Process extends Controller {
	function Process()
	{
		parent::Controller();	
		$this->load->model('user_model');
	}

	function index()
	{
	    if ($this->session->userdata('logged_in') == FALSE)
	    {
			redirect('/admin/');
	    }
		else
		{
			//var_dump($_POST);
			$arr=$_POST;
			switch ($arr['component']) 
			{
				case "com_config":
					$this->com_config($arr);
					break;
				case "com_menu":
					$this->com_menu($arr);
					break;					
				case "com_feature":
					$this->com_feature($arr);
					break;				
				case "com_users":
					$this->com_users($arr);
					break;
				case "com_article":
					$this->com_article($arr);
					break;
				case "com_product":
					$this->com_product($arr);
					break;
				case "com_shopping":
					$this->com_shopping($arr);
					break;					
					
				default:
					echo "You can't access this";
					break;
			}
		}
	}
	
// USERS MANAGER
	function com_users($arr) 
	{
		//echo "<br>".$arr['option']."/".$arr['task'];
			switch ($arr['option']) 
			{
				case "add_user":
					$this->add_user($arr);
					break;
				case "update_user":
					$this->update_user($arr);
					break;					
				case "delete_user":
					$this->delete_user($arr['id']);
					break;				
				case "changepassword":
					$this->change_password($arr['id'],$arr['newpassword'],$arr['myurl']);
					break;
				case "forgotten_password":
					$this->forgotten_password($arr);
					break;
				default:
					echo "You can't access this";
					break;
			}
		
	}
	function add_user()
	{
		$data['username'] = xss_clean($this->input->post('username'), $charset = 'ISO-8859-1');
		$data['password'] = md5($this->input->post('password').'dkw');
		$data['fullname'] = $this->input->post('fullname');
		$data['birthdate'] = $this->input->post('birthdate');
		$data['email'] = $this->input->post('email');
		$data['phone'] = $this->input->post('phone');
		$data['address'] = $this->input->post('address');
		$data['lastlogin'] = date("Y-m-d h:i:s");
		$data['ua_group_id'] = $this->input->post('group');	
		$data['active'] = 1; // if require email confirm --> value=0	
		
		//echo $data['birthdate']."/".$data['lastlogin'];	
		$this->user_model->insert_new_user($data);
	}
	function update_user()
	{

	}
	function delete_user()
	{

	}
	function change_password($user_ID,$newpassword,$myurl)
	{
		//echo $user_ID."/".$newpassword;
		//echo $this->user_model->change_password($user_ID,$newpassword);
		if ($this->user_model->change_password($user_ID,$newpassword))
		{
			$this->session->unset_userdata('logged_in');
	    	redirect('/admin/');
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="message">'.$this->lang->line('action_error').'</div>');
			redirect($myurl);
		}
	}
	function forgotten_password()
	{

	}
	
//-------------- END USERS MANAGER	-----------------------

// CONFIG MANAGER
	function com_config($arr) 
	{
		$this->load->helper('file');
		//$a=read_file('./readfile.txt');
		//$ss=get_filenames('./js/');
		//var_dump ($ss);
		//echo unserialize($a);
		$websitename=str_replace("'","&acute;",$arr['websitename']);
		$websitename=str_replace('"',"&quot;",$arr['websitename']);
		
		$address=str_replace("'","&acute;",$arr['address']);
		$address=str_replace('"',"&quot;",$arr['address']);
		
		$keyword_site=str_replace("'","&acute;",$arr['keyword']);
		$keyword_site=str_replace('"',"&quot;",$arr['keyword']);
		
		$data='<?php $websitename_vn="'.$arr['websitename_vn'].'"; $websitename="'.$websitename.'"; $email_vn="'.$arr['email_vn'].'"; $email="'.$arr['email'].'"; $phone_vn="'.$arr['phone_vn'].'"; $phone="'.$arr['phone'].'"; $fax_vn="'.$arr['fax_vn'].'"; $fax="'.$arr['fax'].'";$address_vn="'.$arr['address_vn'].'"; $address="'.$address.'";$keyword_site_vn="'.$arr['keyword_vn'].'"; $keyword_site="'.$keyword_site.'";';
		write_file('./config.php', $data,'w+');
		redirect($arr['myurl']);
		//var_dump($_POST);
	}
//--------------  END CONFIG MANAGER -------------- 	

}
